/**
 * 
 */
package leetCode;

import java.util.HashMap;
import java.util.Map.Entry;

/**
 * @author zhongfang
 *
 */
public class LongestHarmoniousSubsequence {
    public static int findLHS(int[] nums) {
        HashMap<Integer, Integer> freq=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
			if(freq.containsKey(nums[i])){
				freq.put(nums[i], freq.get(nums[i])+1);
			}else {
				freq.put(nums[i], 1);
			}
		}
        int maxF=0;
        for (Entry<Integer, Integer> entry : freq.entrySet()) {
			int n=entry.getKey();
			int tmp=entry.getValue();
			if(freq.containsKey(n+1)){
				tmp+=freq.get(n+1);
				if(maxF<tmp){
					maxF=tmp;
				}
			}
		}
        return maxF;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] nums={1,3,2,2,5,2,3,7};
		System.out.println(findLHS(nums));
	}

}
